const db = require('../db/index')

exports.anthor_art=(req,res)=>{
    const sql = `select * from anthor_article order by id desc`
    db.query(sql,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('获取文章失败')
        res.send({
            status:200,
            message:'获取文章成功',
            data:results
        })
    })
}

exports.getAnthor_message=(req,res)=>{
    const sql = `select * from leave_message where if_reply = 0`
    db.query(sql,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('获取失败')
        res.send({
            status:200,
            message:'获取成功',
            data:results
        })
    })
}

exports.delmessage=(req,res)=>{
    const sql = `update leave_message set if_reply = 1 where id =?`
    db.query(sql,req.params.id,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('删除失败')
        res.send({
            status:200,
            message:'删除成功'
        })
    })
}

exports.replied_message=(req,res)=>{
    const sql = `select * from leave_message where if_reply = 1`
    db.query(sql,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('获取失败')
        res.send({
            status:200,
            message:'获取成功',
            data:results
        })
    })
}


exports.addAnthor_art=(req,res)=>{
    const sql = `insert into anthor_article set ?`
    db.query(sql,req.body,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('新增失败')
        res.send({
            status:200,
            message:'新增成功'
        })
    })
}

exports.addAnthor_message=(req,res)=>{
    const sql = `insert into leave_message set ?`
    db.query(sql,req.body,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('新增失败')
        res.send({
            status:200,
            message:'新增成功'
        })
    })
}


exports.checkEmail=(req,res)=>{
    const sql = `select * from  leave_message  where userEmail =?`
    db.query(sql,req.params.userEmail,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('查询失败')
        res.send({
            status:200,
            message:'查询成功',
            data:results
        })
    })
}

exports.replied=(req,res)=>{
    const sql = `update leave_message set ? where id =?`
    db.query(sql,[req.body,req.body.id],(err,results)=>{
        if(err) return res.fail(err)
        if(results.affectedRows !== 1) return res.fail('更新失败')
        res.send({
            status:200,
            message:'更新成功',
        })
    })
}


exports.personTarget=(req,res)=>{
    const sql = `select * from Target where isComplete = 0`
    db.query(sql,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('获取失败')
        res.send({
            status:200,
            message:'获取成功',
            data:results
        })
    })
}


exports.addTarget=(req,res)=>{
    const sql = `insert into Target set ?`
    db.query(sql,req.body,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('新增失败')
        res.send({
            status:200,
            message:'新增成功'
        })
    })
}

exports.updateTarget=(req,res)=>{
    const sql = `update Target set isComplete = 1 where targets = ?`
    db.query(sql,req.params.targets,(err,results)=>{
        if(err) return res.fail(err)
        if(results.affectedRows !== 1) return res.fail('更新失败')
        res.send({
            status:200,
            message:'更新成功',
        })
    })
}

exports.domyTargets=(req,res)=>{
    const sql = `select * from Target where isComplete = 1`
    db.query(sql,(err,results)=>{
        if(err) return res.fail(err)
        if(results === '') return res.fail('获取失败')
        res.send({
            status:200,
            message:'获取成功',
            data:results
        })
    })
}